//
//  IGSearchHotKeyCell.m
//  jingdongfang
//
//  Created by BOE on 2017/8/28.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGSearchHotKeyCell.h"
#import "UIImage+IDPExtension.h"

@implementation IGSearchHotKeyCell

+ (IGSearchHotKeyCell *)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
    IGSearchHotKeyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kIGSearchHotKeyCell forIndexPath:indexPath];
    if (!cell) {
        cell = [[IGSearchHotKeyCell alloc] init];
    }
    [cell createUI];
    return cell;
}

- (void)createUI {
    self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchBtn.titleLabel.font = [UIFont font14];
    self.searchBtn.layer.masksToBounds = YES;
    self.searchBtn.layer.cornerRadius = 15.0;
    self.searchBtn.layer.borderWidth = 1.0;
    self.searchBtn.layer.borderColor = [UIColor c5Color].CGColor;
    [self.searchBtn setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
    [self.searchBtn setTitleColor:[UIColor c3Color] forState:UIControlStateHighlighted];
    UIImage *image = [UIImage createImageWithColor:[UIColor c1Color]
                                              size:CGSizeMake(1, 1)];
    [self.searchBtn setBackgroundImage:image forState:UIControlStateHighlighted];
    [self.contentView addSubview:self.searchBtn];
    __weak typeof(self) weakSelf = self;
    [self.searchBtn setTapActionWithBlock:^{
        if (weakSelf.block) {
            weakSelf.block(weakSelf.hotKey);
        }
    }];
    
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)setHotKey:(NSString *)hotKey {
    _hotKey = hotKey;
    [self.searchBtn setTitle:[NSString stringWithFormat:@"%@", hotKey] forState:UIControlStateNormal];
}

- (UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes {
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    CGSize size = [self.contentView systemLayoutSizeFittingSize: layoutAttributes.size];
    CGRect cellFrame = layoutAttributes.frame;
    cellFrame.size.height= size.height;
    layoutAttributes.frame= cellFrame;
    return layoutAttributes;
}

@end
